package com.atguigu.blog.controller;

import com.atguigu.blog.entity.Category;
import com.atguigu.blog.service.CategoryService;
import com.atguigu.blog.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 1.2 文章类型展现
     * @author Petrichor
     * @date 2023/10/23 16:10
     **/
    @GetMapping("/getCategory")
    public Result getCategory(){
        List<Category> categoryList = categoryService.getCategory();
        if(categoryList.size() > 0){
            return Result.ok(categoryList);
        }else{
            return Result.fail();
        }
    }
    /**
     * 4.1 分类列表
     * @author Petrichor
     * @date 2023/10/24 18:04
     **/
    @GetMapping("/findCategoryByPage")
    public Result findCategoryByPage(int page, int limit){

        PageInfo info = categoryService.findCategoryByPage(page,limit);
        return Result.ok(info);
    }
    /**
     * 4.2 根据名称查询分类信息
     * @author Petrichor
     * @date 2023/10/24 18:36
     **/
    @GetMapping("/findCategoryByName/{cname}")
    public Result findCategoryByName(@PathVariable String cname){

        List<Category> categoryList = categoryService.findCategoryByName(cname);
        return Result.ok(categoryList);
    }
    /**
     * 4.3 文章分类新增
     * @author Petrichor
     * @date 2023/10/24 19:58
     **/
    @PostMapping("/saveCategory")
    public Result saveCategory(@RequestBody Category category){
        categoryService.saveCategory(category);
        return Result.ok();
    }
    /**
     * 4.4 文章分类修改
     * @author Petrichor
     * @date 2023/10/24 19:58
     **/
    @PutMapping("/updateCategory")
    public Result updateCategory(@RequestBody Category category){
        categoryService.updateCategory(category);
        return Result.ok();
    }
    /**
     * 4.5 文章分类删除
     * @author Petrichor
     * @date 2023/10/24 20:22
     **/
    @DeleteMapping("/deleteCategoryById")
    public Result deleteCategoryById(Long id){
        System.out.println("删除==========================");
        categoryService.deleteCategoryById(id);
        return Result.ok();
    }
}
